#define _CRT_SECURE_NO_WARNINGS
#include "Queue.h"

typedef int BTreeDataType;

typedef struct BTreeNode
{
	BTreeDataType val;
	struct BTreeNode* left;
	struct BTreeNode* right;
}BTreeNode;

BTreeNode* BuyBTreeNode(BTreeDataType x)
{
	BTreeNode* NewBTreeNode = (BTreeNode*)malloc(sizeof(BTreeNode));
	if (NewBTreeNode == NULL)
	{
		perror("malloc");
		return NULL;
	}
	NewBTreeNode->val = x;
	NewBTreeNode->left = NULL;
	NewBTreeNode->right = NULL;
	return NewBTreeNode;
}

void LevelOrder(BTreeNode* root)
{
	Queue q;
	QInit(&q);
	if (root == NULL)
	{
		return;
	}
	QPush(&q, root);
	while (!QEmpty(&q))
	{
		BTreeNode* front = QFront(&q);
		QPop(&q);
		printf("%d ", front->val);
		if (front->left)
		{
			QPush(&q,front->left);
		}
		if (front->right)
		{
			QPush(&q,front->right);
		}
	}
	QDestroy(&q);
}

int main()
{
	BTreeNode* node1 = BuyBTreeNode(2);
	BTreeNode* node2 = BuyBTreeNode(5);
	BTreeNode* node3 = BuyBTreeNode(1);
	BTreeNode* node4 = BuyBTreeNode(3);
	BTreeNode* node5 = BuyBTreeNode(6);
	BTreeNode* node6 = BuyBTreeNode(4);

	node1->left = node2;
	node1->right = node3;
	node3->left = node4;
	node4->right = node5;
	node5->right = node6;

	LevelOrder(node1);

	return 0;
}